﻿<%@ WebHandler Language="C#" Class="UserLinkProcess" %>

using System;
using System.Web;
using System.Web.SessionState;

public class ServiceInfo
{
    public string message { get; set; }

    public String data { get; set; }
}

public class UserInfo
{
    public string user_id { get; set; }

    public string app_user_name { get; set; }

    public string unitid { get; set; }

    public string app_user_pwd { get; set; }
    
    public string key { get; set; }
}

public class UserLinkProcess : IHttpHandler, IRequiresSessionState
{
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    
    /// <summary>
    /// 用户单点登录校验
    /// </summary>
    /// <param name="context"></param>
    public void ProcessRequest(HttpContext context)
    {
        string strReturn = string.Empty;

        try
        {
            //从url获取认证code
            String code = context.Request["code"];
            String url = context.Request["m_url"];
            //实例化OAuthClient类
            OAuthClient oc = new OAuthClient(
                //服务器回调地址
                System.Configuration.ConfigurationManager.AppSettings["OAUTH_SERVER_URI"],
                System.Configuration.ConfigurationManager.AppSettings["APP_ID"],
                System.Configuration.ConfigurationManager.AppSettings["APP_KEY"]
                );
            //获取服务器传回的token（json的字符串），里面包含了办事大厅用户id，业务系统用户名，关联的企业id
            String token = oc.getAccessToken(code);
            //string token = "{'message':'1','data':\"{\'user_id\':\'lonesafe\',\'app_user_name\':\'syn_lonesafe\',\'unitid\':\'EC07C3FF2B7D4043B9B1B2ED7CB1F022\'}\"}";


            //验证是否是管理员
            if (!string.IsNullOrEmpty(token))
            {
                ServiceInfo siTemp = Common.JsonOperation.GetObjectFromJson<ServiceInfo>(token);
                if (siTemp.message == "1")
                {
                    UserInfo uiTemp = Common.JsonOperation.GetObjectFromJson<UserInfo>(siTemp.data);
                    HttpContext.Current.Session["title"] = "国家测绘地理信息局行政许可在线审批系统";
                    HttpContext.Current.Session.Remove("Menu");

                    //对预审标志初始化
                    HttpContext.Current.Session["PromiseFlag"] = string.Empty;
                    HttpContext.Current.Session["UserID"] = uiTemp.app_user_name;
                    string strUserName = ChBusiness.User.UserHandle.GetNotes(uiTemp.app_user_name);

                    if (!string.IsNullOrEmpty(strUserName))
                    {
                        HttpContext.Current.Session["LoginUserName"] = strUserName;
                        //context.Response.Redirect("../MainPage.htm");
                        context.Response.Redirect(url);
                    }
                    else
                    {
                        string strScript = string.Format("<script>alert('{0}用户在业务系统中不存在')</script>",uiTemp.app_user_name);
                        context.Response.Write(strScript);
                    }
                }
                else
                {
                    Common.Log.LogManage.addLogErr("获取token信息失败", token);
                    string strScript = "<script>alert('获取token信息失败,请咨询办事大厅技术支持')</script>";
                    context.Response.Write(strScript);
                }
            }
            else
            {
                context.Response.Write("token为空");
            }
        }
        catch(Exception ex)
        {
            Common.Log.LogManage.addLogErr("valide", ex.Message);
            context.Response.Write("信息校验失败");
        }
    }

}